package test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import util.Constant;

import java.io.File;

public class FilesCopy {
    public static void main(String args[]){

        // 源路径
        String sourcePath = "/Users/xujiangfei/Downloads/elaAdmin";
        // 目标路径
        String targetPath = "/Users/xujiangfei/Downloads/my";

        System.out.println("-----------------------");
        System.out.println("--开始拷贝");
        System.out.println("-----------------------");

        dealWithAllFile(new File(sourcePath),targetPath);

        System.out.println("-----------------------");
        System.out.println("--拷贝结束");
        System.out.println("-----------------------");
    }

    /**
     * 递归处理所有文件
     * @param file 文件夹File
     * @param targetPath 目标路径Path
     */
    private static void dealWithAllFile(File file,String targetPath) {

        copyFileByExt(file,targetPath);

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if(files!=null){
                for (File f : files) {
                    dealWithAllFile(f,targetPath);
                }
            }
        }
    }

    /**
     * 根据后缀copy到指定文件夹
     * @param source 源文件
     * @param targetPath 目标文件夹路径
     */
    private static void copyFileByExt(File source,String targetPath){
        if(Constant.HTML.equals(FileUtil.extName(source.getAbsolutePath()).toLowerCase())){
            FileUtil.copy(source.getAbsolutePath(),targetPath + Constant.HTMLPATH + source.getName(),Constant.ISOVERRIDE);
        }else if(Constant.CSS.equals(FileUtil.extName(source.getAbsolutePath()).toLowerCase())){
            FileUtil.copy(source.getAbsolutePath(),targetPath + Constant.CSSPATH + source.getName(), Constant.ISOVERRIDE);
        }else if(Constant.JS.equals(FileUtil.extName(source.getAbsolutePath()).toLowerCase())){
            FileUtil.copy(source.getAbsolutePath(),targetPath + Constant.JSPATH + source.getName(), Constant.ISOVERRIDE);
        }else if(!StrUtil.hasEmpty(FileUtil.extName(source.getAbsolutePath())) && Constant.IMG.contains(FileUtil.extName(source.getAbsolutePath()).toLowerCase())){
            FileUtil.copy(source.getAbsolutePath(),targetPath + Constant.IMGPATH + source.getName(), Constant.ISOVERRIDE);
        }else if(!StrUtil.hasEmpty(FileUtil.extName(source.getAbsolutePath())) && Constant.FONT.contains(FileUtil.extName(source.getAbsolutePath()).toLowerCase())){
            FileUtil.copy(source.getAbsolutePath(),targetPath + Constant.FONTPATH + source.getName(), Constant.ISOVERRIDE);
        }else if(!StrUtil.hasEmpty(FileUtil.extName(source.getAbsolutePath()))){
            FileUtil.copy(source.getAbsolutePath(),targetPath + Constant.OTHERPATH + source.getName(), Constant.ISOVERRIDE);
        }

    }
}
